<?php
class url
{
    public static $array    = array();
    public static $string   = '';
    public static $method   = '';

    public static $controller   = '';
    public static $action       = '';
    public static $id           = '';
    public static $params       = array();

    /**
     * @static get request data and pars it
     */
    public static function get()
    {
        self::$string   = $_SERVER['REQUEST_URI'];
        self::$method   = $_SERVER['REQUEST_METHOD'];

        self::$array    = explode('/', self::$string);

        // todo add data testing
        self::$controller   = self::$array['1'];
        self::$method       = self::$array['2'];
        self::$id           = self::$array['3'];

        // todo add data testing
        self::$params       = $_GET;
    }

    /**
     * @static redirect to $url
     * @param $url - string to redirect to
     */
    public static function redirect($url = '/')
    {
        // todo add data tesing
        header('Location: '.$url);
    }

    /**
     * @static generate url to redirect to from array
     * @param array $cai - array('controller' => 'cnt', 'action' => 'act', 'id' => 'num')
     * @return bool
     */
    public static function redirect_to($cai = array())
    {
        if ($link = self::cai_to_url($cai))
        {
            header('Location: '.$link);
            exit;
        }
        return false;
    }

    /**
     * @static generate url from array
     * @param array $cai - array('controller' => 'cnt', 'action' => 'act', 'id' => 'num')
     * @return bool|string
     */
    public static function cai_to_url($cai = array())
    {
        if (!empty($cai['controller']) && !empty($cai['model']) && !empty($cai['id']))
        {
            return '/'.$cai['controller'].'/'.$cai['model'].'/'.$cai['id'];
        }
        return false;
    }
}
